{% set pypi_name = 'monasca-agent' %}
{% set source = fetch_source('https://tarballs.openstack.org/monasca-agent/monasca-agent-master.tar.gz') %}
{% set upstream_version = upstream_version() %}
{% set rpm_release = '1' %}

%define username            monasca-agent
%define groupname           monasca
%if 0%{?rhel} || 0%{?fedora}
%global rdo 1
%endif
Name:           {{ py2name() }}
Version:        {{ py2rpmversion() }}
Release:        {{ py2rpmrelease() }}
Summary:        OpenStack Monasca metrics agent
License:        {{ license('Apache-2.0') }}
Group:          Development/Languages/Python
URL:            https://wiki.openstack.org/wiki/Monasca
Source0:        {{ source|basename }}
Source1:        %{name}.sudoers
Source2:        %{name}.target
Source3:        %{name}.tmpfiles
Source4:        %{name}.polkit
Source5:        %{name}-collector.service
Source6:        %{name}-forwarder.service
Source7:        %{name}-statsd.service
BuildRequires:  fdupes
BuildRequires:  openstack-macros
BuildRequires:  {{ py3('PyYAML') }}
BuildRequires:  {{ py3('eventlet') }}
BuildRequires:  {{ py3('freezegun') }}
BuildRequires:  {{ py3('httplib2') }}
BuildRequires:  {{ py3('python-keystoneclient') }}
BuildRequires:  {{ py3('mock') }}
BuildRequires:  {{ py3('monasca-common') }}
BuildRequires:  {{ py3('python-monascaclient') }}
BuildRequires:  {{ py3('netaddr') }}
BuildRequires:  {{ py3('ntplib') }}
BuildRequires:  {{ py3('oslo.concurrency') }}
BuildRequires:  {{ py3('oslo.config') }}
BuildRequires:  {{ py3('oslo.i18n') }}
BuildRequires:  {{ py3('oslo.middleware') }}
BuildRequires:  {{ py3('oslo.utils') }}
BuildRequires:  {{ py3('oslo.vmware') }}
BuildRequires:  {{ py3('oslotest') }}
BuildRequires:  {{ py3('psutil') }}
BuildRequires:  {{ py3('pymongo') }}
BuildRequires:  {{ py3('python-memcached') }}
BuildRequires:  {{ py3('redis') }}
BuildRequires:  {{ py3('requests') }}
BuildRequires:  {{ py3('stevedore') }}
BuildRequires:  {{ py3('testrepository') }}
BuildRequires:  {{ py3('tornado') }}
BuildRequires:  sudo
Requires:       python3-{{ pypi_name }} = %{version}-%{release}
BuildArch:      noarch
%if 0%{?suse_version}
BuildRequires:  systemd-rpm-macros
%{?systemd_requires}
%else
BuildRequires:  systemd
Requires(post): systemd
Requires(postun):   systemd
Requires(preun):    systemd
%endif

%description
The Monasca Agent is a modern Python monitoring agent for gathering metrics and
sending them to the Monasca API.

%package -n python3-{{ pypi_name }}
Summary:        Python module for monasca-agent
Group:          Development/Languages/Python
Requires:       {{ py3('PyYAML') }}
Requires:       {{ py3('eventlet') }}
Requires:       {{ py3('httplib2') }}
Requires:       {{ py3('keystoneauth1') }}
Requires:       {{ py3('python-keystoneclient') }}
Requires:       {{ py3('monasca-common') }}
Requires:       {{ py3('python-monascaclient') }}
Requires:       {{ py3('netaddr') }}
Requires:       {{ py3('python-neutronclient') }}
Requires:       {{ py3('python-novaclient') }}
Requires:       {{ py3('ntplib') }}
Requires:       {{ py3('oslo.concurrency') }}
Requires:       {{ py3('oslo.config') }}
Requires:       {{ py3('oslo.i18n') }}
Requires:       {{ py3('oslo.utils') }}
Requires:       {{ py3('oslo.vmware') }}
Requires:       {{ py3('psutil') }}
Requires:       {{ py3('pymongo') }}
Requires:       {{ py3('python-memcached') }}
Requires:       {{ py3('redis') }}
Requires:       {{ py3('requests') }}
Requires:       {{ py3('six') }}
Requires:       {{ py3('stevedore') }}
# FIXME(jpena): Remove this conditional once python-toml is available in RDO
%if 0%{?rdo}
Requires:       {{ py3('tornado') }}
%else
Requires:       {{ py3('toml') }}
Requires:       {{ py3('tornado') }}
%endif

%description -n python3-{{ pypi_name }}
The Monasca Agent is a modern Python monitoring agent for gathering metrics and
sending them to the Monasca API.

This package contains monasca-agent Python module.

%prep
%autosetup -p1 -n {{ pypi_name }}-{{ upstream_version }}
%py_req_cleanup
# contains various binary files etc that we don't need
rm -rf packaging/monasca-agent{,-deb}

%build
%{py3_build}

%install
%{py3_install}

# /etc
install -d -m 755 %{buildroot}%{_sysconfdir}/monasca
install -d -m 750 %{buildroot}%{_sysconfdir}/monasca/agent/conf.d

# /usr/lib
install -d -m 755 %{buildroot}%{_prefix}/lib/monasca/agent/custom_checks.d
install -d -m 755 %{buildroot}%{_prefix}/lib/monasca/agent/custom_detect.d

# /var
install -d -m 750  %{buildroot}%{_localstatedir}/log/{{ pypi_name }}

# sudoers configuration
install -D -m 440 %{SOURCE1} %{buildroot}%{_sysconfdir}/sudoers.d/%{name}

# systemd unit file
install -D -m 644 %{SOURCE2} %{buildroot}%{_unitdir}/%{name}.target
install -D -m 644 %{SOURCE5} %{buildroot}%{_unitdir}/%{name}-collector.service
install -D -m 644 %{SOURCE6} %{buildroot}%{_unitdir}/%{name}-forwarder.service
install -D -m 644 %{SOURCE7} %{buildroot}%{_unitdir}/%{name}-statsd.service
%if 0%{?suse_version}
mkdir -p %{buildroot}%{_sbindir}
ln -sr %{buildroot}%{_sbindir}/service %{buildroot}%{_sbindir}/rc%{name}
%endif

# systemd tmpfile
install -D -m 644 %{SOURCE3} %{buildroot}%{_tmpfilesdir}/openstack-monasca-agent.conf

# polkit permissions
install -D -m 644 %{SOURCE4} %{buildroot}%{_sysconfdir}/polkit-1/rules.d/49-monasca-agent.rules

%fdupes %{buildroot}%{python3_sitelib}

%pre
# create user and groups
%openstack_pre_user_group_create %{username} %{groupname}

%post
%tmpfiles_create %{_tmpfilesdir}/openstack-monasca-agent.conf
%systemd_post %{name}.target
%systemd_post %{name}-collector.service
%systemd_post %{name}-forwarder.service
%systemd_post %{name}-statsd.service

%preun
%systemd_preun %{name}.target
%systemd_preun %{name}-collector.service
%systemd_preun %{name}-forwarder.service
%systemd_preun %{name}-statsd.service

%postun
%systemd_postun %{name}.target
%systemd_postun %{name}-collector.service
%systemd_postun %{name}-forwarder.service
%systemd_postun %{name}-statsd.service

%check
find . -type f -name *.pyc -delete
find . -type f -name '*test_service_plugin.py' -delete
python3 -m stestr.cli run

%files
%dir %attr(-, root, monasca) %{_sysconfdir}/monasca
%dir %attr(0750, %{username}, %{groupname}) %{_sysconfdir}/monasca/agent
%dir %attr(0750, %{username}, %{groupname}) %{_sysconfdir}/monasca/agent/conf.d
%dir %attr(0755, root, root) %{_prefix}/lib/monasca/agent/custom_checks.d
%dir %attr(0755, root, root) %{_prefix}/lib/monasca/agent/custom_detect.d
%_tmpfilesdir/openstack-monasca-agent.conf
%config %{_sysconfdir}/monasca/agent
%config %{_sysconfdir}/sudoers.d/%{name}
%dir %{_prefix}/lib/monasca
%{_prefix}/lib/monasca/agent
%dir %attr(0750, %{username}, %{groupname}) %{_localstatedir}/log/{{ pypi_name }}
%{_unitdir}/%{name}.target
%{_unitdir}/%{name}-collector.service
%{_unitdir}/%{name}-forwarder.service
%{_unitdir}/%{name}-statsd.service
%if 0%{?suse_version}
%{_sbindir}/rc%{name}
%endif
%{_sysconfdir}/polkit-1/
%{_sysconfdir}/polkit-1/rules.d/
%{_sysconfdir}/polkit-1/rules.d/49-monasca-agent.rules

%files -n python3-{{ pypi_name }}
%doc README.rst
%license LICENSE
%{_bindir}/monasca-collector
%{_bindir}/monasca-forwarder
%{_bindir}/monasca-setup
%{_bindir}/monasca-statsd
%{python3_sitelib}/monasca_agent
%{python3_sitelib}/monasca_agent-*.egg-info
%{python3_sitelib}/monasca_setup
%dir %{_datadir}/monasca
%{_datadir}/monasca/agent

%changelog
